package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class Selector6 extends AxdrType {
    private byte[] dataCode = null;
    private DateTimeS dtss = null;
    private DateTimeS dtse = null;
    private TI inter = null;
    private MS ms = null;

    public Selector6() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public DateTimeS getDtss() {
        return dtss;
    }

    public DateTimeS getDtse() {
        return dtse;
    }

    public TI getInter() {
        return inter;
    }

    public MS getMs() {
        return ms;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(DateTimeS dtss, DateTimeS dtse, TI inter, MS ms) {
        this.dtss = dtss;
        this.dtse = dtse;
        this.inter = inter;
        this.ms = ms;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.dtss = new DateTimeS();
        codeLength += this.dtss.decode(input);
        this.dtse = new DateTimeS();
        codeLength += this.dtse.decode(input);
        this.inter = new TI();
        codeLength += this.inter.decode(input);
        this.ms = new MS();
        codeLength += this.ms.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.dtss != null && this.dtse != null && this.inter != null && this.ms != null) {
                codeLength = 0;
                codeLength += this.ms.encode(output);
                codeLength += this.inter.encode(output);
                codeLength += this.dtse.encode(output);
                codeLength += this.dtss.encode(output);
            } else {
                throw new RuntimeException("dtss or dtse or ms or inter is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.dtss != null && this.dtse != null && this.ms != null && this.inter != null) {
            return "sequence: {" + "dtss: " + this.dtss + ", dtse: " + this.dtse + ", inter: " + this.inter + ", ms: " + this.ms + "}";
        } else {
            return "dtss or dtse or ms or inter is null";
        }
    }
}



